package niuke;

import java.util.Scanner;

public class 连续最大和 {

    static int[] numbers;
    static int n;
    static int max = -999999999;
    static int start = 0;
    static int end = 0;
    static int sum = 0;
    static int flag;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        numbers = new int[n];
        for(int i=0;i<n;i++){
            numbers[i] = scanner.nextInt();
        }
        f();
        System.out.println(max);
    }

    public static void f(){
        //从前往后遍历判断
        for(int i=1;i<n;i++){
            if(judge(i)){
                end++;
                continue;
            }
            else {
                add();
                start = i;
                end = i;
            }
        }
        if(end - start >0){
            add();
        }
    }

    //判断是否连续
    public static boolean judge(int p){
        int x = numbers[p] - numbers[p-1];
        if(x==1 || x==-1){
            if(flag==0){
                flag = x;
                return true;
            }
            else {
                if(x == flag){
                    return true;
                }
                else {
                    return false;
                }
            }
        }
        else {
            return false;
        }
    }

    //判断连续后  做加法
    public static void add(){
        for(int i=start;i<=end;i++){
            if(numbers[i]>0){
                sum = sum + numbers[i];
            }
        }
        if(sum>max){
            max = sum;
        }
        sum = 0;
    }
}
